using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace Test {
	class Search {

		static void MainX() {
			List<string> exes=SearchFilesRecursive(@"C:\windows","*.ini");
		}


		public static List<string> SearchFilesRecursive(string directory,string pattern) {
			Queue<string> stack=new Queue<string>();
			List<string> result=new List<string>();
			stack.Enqueue(directory);
			List<string> current_dir=new List<string>();
			int extention_length=pattern.Length-1;
			while(stack.Count>0)
			{
				string dir=stack.Dequeue();
				try
				{
					current_dir.AddRange(Directory.GetFiles(dir,pattern));
					for(int i=0;i<current_dir.Count;i++)
					{
						if(current_dir[i].LastIndexOf(".")!=current_dir[i].Length-extention_length)
						{
							current_dir.RemoveAt(i);
							i--;
						}
					}
					result.AddRange(current_dir);
					current_dir.Clear();

					// Add all directories at this directory.
					foreach(string dn in Directory.GetDirectories(dir))
					{
						stack.Enqueue(dn);
					}
				}
				catch(Exception ex)
				{
					Console.WriteLine(ex.Message);
					// Couldn't open the directory
				}
			}
			return result;
		}
	}
}
